﻿using CPPEI.App.Console;
using CPPEI.BD.Contract;
using CPPEI.Coolzon.Common.Startup;
using CPPEI.Coolzon.Configuration;
using CPPEI.Net.UDP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.BD.RemoteSender
{
    /// <summary>
    /// 发送数据到远程的agent程序，让agent程序执行特定的工作
    /// RemoteSender.exe /run 'abc.exe /solution s1'
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            new App().Run(args);
        }
    }

    class App : XApplication
    {
        UDPClient _udpClient;

        public App()
            : base("CPPEI", "BD", "RemoteSender", "RemoteSender")
        {
            Settings.IsAutoRun = false;
            Settings.IsSingleton = false;
        }

        protected override bool OnXStartup(StartupParamCollection startupParams)
        {
            string solutionName = startupParams.Get("solution").Value;
            byte[] data = Convert.FromBase64String(solutionName);

            string targetIP = System.Configuration.ConfigurationManager.AppSettings["TargetIP"];
            int targetPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TargetPort"]);

            _udpClient = new UDPClient(targetPort, new ResponseHandlerFactory());
            Net.NetPackage package = new Net.NetPackage(NETCMD.RunVMTool, Net.NetReplayMode.None, 0, data);
            _udpClient.SendTo(targetIP, package);
            return false;
        }
    }
}
